package com.wwj.gulimall.product.service.impl;

import com.wwj.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.common.utils.PageUtils;
import com.wwj.common.utils.Query;

import com.wwj.gulimall.product.dao.CategoryDao;
import com.wwj.gulimall.product.entity.CategoryEntity;
import com.wwj.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 查出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        // 组装成父子的树形结构

        // 2.1 找到所有的一级分类
        List<CategoryEntity> level1Menus = categoryEntities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu)->{
            menu.setChildren(getChildrens(menu,categoryEntities));

            //  2.2 找到所有的二级目录
//            List<CategoryEntity> children = menu.getChildren();
//            for (CategoryEntity entity : children) {
//                entity.setChildren(getChildrens(entity,categoryEntities));
//            }

            return menu;
        }).sorted((menu1, menu2)-> {
            return menu1.getSort() - menu2.getSort();
        }).collect(Collectors.toList());


        // 2.2 找到所有的二级目录


        return level1Menus;
    }

    @Override
    public void removeMenuByIds(List<Long> list) {
        //TODO 1.检查当前删除的菜单，是否被别的地方引用
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        findParentPath(catelogId, path);
        return path.toArray(new Long[path.size()]);
    }

    @Transactional
    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        if (!StringUtils.isEmpty(category.getName())){
            categoryBrandRelationService.updateCatelog(category.getCatId(),category.getName());
        }
    }

    // 递归找到所有的父节点id
    private void findParentPath(Long catelogId, List<Long> path){
        path.add(catelogId);
        CategoryEntity category = this.getById(catelogId);
        if (category.getParentCid() != 0){
            findParentPath(category.getParentCid(), path);
        }
        Collections.reverse(path);
    }

    // 递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> Childrens = all.stream().filter((one) ->
                        one.getParentCid().equals(root.getCatId())
                ).map((menu)->{
                    menu.setChildren(getChildrens(menu,all));
                    return menu;
                }).sorted((menu1, menu2) -> {
                    return (menu1.getSort()==null?0: menu1.getSort())-
                            (menu2.getSort()==null?0: menu2.getSort());
                }).collect(Collectors.toList());
        return Childrens;
    }

}